package andy.demo.date;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.text.ParseException;
import java.text.SimpleDateFormat;




//javac  -encoding utf-8  CalendarTest.java
public class CalendarTest {
    static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
    /**
     * 指定日期格式 yyyy-MM-dd HH:mm:ss
     */
    public static final String DATE_FORMAT_2 = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_2_A = "MM-dd HH:mm";
    public static final Locale MYLOCALE = Locale.ENGLISH;
    
    public static void main(String[] args) {
        if (args == null || args.length < 2) {
            System.out.println("No parameters found!");
            return;
        }

        String txtDate = args[0];
        int lenth = Integer.parseInt(args[1]);
        String txtDate2 = args[2];

        try {

            Date start = dateFormat.parse(txtDate);
            
            //test add
            System.out.println("The Input Date: " + dateFormat.format(start));// 20120601

            //Date end = addDate(start, lenth);
            Date end = addMinute(start, lenth);

            System.out.println("The Added Date: " + dateFormat.format(end));
            //---end
            
            
            System.out.println("matchAfter: " + matchAfter("2013-11-5 11:46:00"));


            Date current = dateFormat.parse(txtDate2);
            
            
//            int ret = compareDate(start, lenth, current);
//            System.out.println("ret=: " + ret);
            
            Date resultDate = randomDate(txtDate, txtDate2);
            System.out.println("ret=: " + dateFormat.format(resultDate));
            System.out.println("current=: " + isToday(start));
            System.out.println("createTodayDate=: " + createTodayDate(23, 0, 0));
            System.out.println("isTimeMatch=: " + isTimeMatch(start));
            
            //获取随机数
            List<Integer> radomSnList = new ArrayList<Integer>();
            
            ArrayList<Integer> datas = new ArrayList<Integer>();
            datas.add(1);
            datas.add(6);
            radomSnList.add(getRandom(datas));
            System.out.println("radomSnList=: " + radomSnList);
            
            
            System.out.println("formatDate2_A=: " + formatDate(new Date(), DATE_FORMAT_2_A));
            
            
            

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        
        
        

    }

    /**
     * @param start
     *            开始日期
     * @param lenth
     *            日期长度
     * @param current
     *            当前日期
     * @return <pre>
     * -1: 区间左侧 
     *  0: 区间内 
     *  1: 区间右侧
     * </pre>
     * 
     *         {@code
     * 
     * compareDate(20121001, 7, 20120929) = -1
     * compareDate(20121001, 7, 20120930) = -1 
     * compareDate(20121001, 7, 20121001) = 0 
     * compareDate(20121001, 7, 20121007) = 0 
     * compareDate(20121001, 7, 20121008) = 1 
     * compareDate(20121001, 7, 20121009) = 1
     * 
     * }
     */
    public static int compareDate(Date start, int lenth, Date current) {

        int ret1 = current.compareTo(start);
        Date end = addDate(start, lenth - 1);
        int ret2 = current.compareTo(end);

        if (ret1 < 0) {
            return -1;
        } else if (ret2 > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    /*
     * 日期加法： 如：addDate(20121001, 7)=20121008 如：addDate(20121031, 2)=20121102
     */
    public static Date addDate(Date start, int lenth) {

        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(start);

        // System.out.println("The Input Date: " +
        // calendar.getTime());//12345678901234
        // System.out.println("The Input Date: " +
        // dateFormat.format(calendar.getTime()));// 20120601

        calendar.add(GregorianCalendar.DATE, lenth);

        // System.out.println("The Added Date: " +
        // dateFormat.format(calendar.getTime()));

        return calendar.getTime();
    }
    
    /**
     * 生成随机时间
     * 
     * @param beginDate
     * @param endDate
     * @return
     */
    private static Date randomDate(String beginDate, String endDate) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss");

            Date start = format.parse(beginDate);// 构造开始日期

            Date end = format.parse(endDate);// 构造结束日期

            // getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
            if (start.getTime() >= end.getTime()) {
                return null;
            }

            long date = random(start.getTime(), end.getTime());

            return new Date(date);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }

    private static long random(long begin, long end) {
        long rtn = begin + (long) (Math.random() * (end - begin));

        // 如果返回的是开始时间和结束时间，则递归调用本函数查找随机值
        if (rtn == begin || rtn == end) {
            return random(begin, end);
        }

        return rtn;
    }

    public static boolean isToday(Date workDay) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        
        Date currTime = new Date();
        
        c1.setTime(workDay);
        c2.setTime(currTime);
        
        if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) 
                && (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))
                && c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH)) {
            return true;
        } else {
            return false;
        }
    }
    
    public static String createTodayDate(int hour, int minute, int second) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_2, MYLOCALE);
        
        Calendar calendar = Calendar.getInstance(MYLOCALE);
        
        calendar.set(Calendar.HOUR, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        
        return format.format(calendar.getTime());
    }
    
    public static boolean isTimeMatch(Date workDay) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        
        Date currTime = new Date();
        
        c1.setTime(workDay);
        c2.setTime(currTime);
        
        if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) 
                && c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH)
                && c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH)
                        && c1.get(Calendar.HOUR_OF_DAY) == c2.get(Calendar.HOUR_OF_DAY)
                ) {
            if (Math.abs(c1.get(Calendar.MINUTE) - c2.get(Calendar.MINUTE)) <= 5) {
                return true;
            }
            return false;
        } else {
            return false;
        }
    }
    
    public static <T> T getRandom(final List<T> readomWords) {
        final int idx = (int) (Math.random() * readomWords.size());
        return readomWords.get(idx);
    }
    
    /**
     * 日期加法<br>
     * 
     * 如：addDate(2012-10-01 23:01:01, 7)=2012-10-01 23:08:01
     * 
     * @param start
     * @param lenth
     * @return
     */
    public static Date addMinute(Date start, int lenth) {

        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(start);

        calendar.add(GregorianCalendar.MINUTE, lenth);

        return calendar.getTime();
    }
    
    public static boolean matchAfter(String after) {
        Date current = addMinute(parseDate(after), 0);

        int ret = compareMinute(new Date(), 3, current);

        return ret == 0 ? true : false;
    }
    
    public static int compareMinute(Date start, int lenth, Date current) {

        int ret1 = current.compareTo(start);
        Date end = addMinute(start, lenth);
        int ret2 = current.compareTo(end);

        System.out.println("start: " + dateFormat.format(start));
        System.out.println("end: " + dateFormat.format(end));
        
        if (ret1 < 0) {
            return -1;
        } else if (ret2 > 0) {
            return 1;
        } else {
            return 0;
        }
    }
    
    /**
     * 解析特定格式的日期:"yyyy-MM-dd HH:mm:ss"
     * 
     * @param date
     * @return
     */
    public static Date parseDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_2, MYLOCALE);
        
        try {
            return format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        
        return null;
    }
    
    /**
     * 格式化日期为特定格式:"yyyy-MM-dd HH:mm:ss"
     * 
     * @param date
     * @return
     */
    public static String formatDate(final Date date) {
        return formatDate(date, DATE_FORMAT_2);
    }

    /**
     * 格式化日期为特定格式template
     * 
     * @param date
     * @param template   
     * @return
     */
    public static String formatDate(final Date date, final String template) {
        SimpleDateFormat formater = new SimpleDateFormat(template, MYLOCALE);
        return formater.format(date);
    }
}
